# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=7.3.0
pkgrel=2
pkgdesc="Cross GCC for the MinGW-w64"
arch=('i686' 'x86_64')
url="https://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("zlib" "mpc" "isl"
         "${_mingw_suff}-binutils"
         "${_mingw_suff}-crt"
         "${_mingw_suff}-headers"
         "${_mingw_suff}-winpthreads"
         "${_mingw_suff}-windows-default-manifest")
makedepends=("gcc" 'lndir' "gmp-devel" "mpc-devel" "zlib-devel" "isl-devel")
#checkdepends=('dejagnu')
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.gz
        0001-share-mingw-fset-stack-executable-with-cygwin.patch
        0007-Avoid-installing-libffi-V2.patch
        0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch
        0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch
        0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch
        0012-handle-dllimport-properly-in-medium-model-V2.patch
        0013-skip-test-for-cygwin-mingw.patch
        0014-64bit-Cygwin-uses-SEH.patch
        0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch
        0016-fix-some-implicit-declaration-warnings.patch
        0017-__cxa-atexit-for-Cygwin.patch
        0018-prevent-modules-from-being-unloaded-before-their-dto.patch
        0019-Cygwin-doesn-t-do-text-mode-translations-for-file-ha.patch
        0020-cygwin-uses-cyg-lib-prefix-v3.patch
        0022-libgomp-soname-cygwin-mingw.patch
        0024-libitm-weak-symbols.patch
        0025-enable-libcilkrts-V2.patch
        0028-g++-time.patch
        0029-gcc-specs.patch
        0030-newlib-ftm.patch
        0031-define_std-unix.patch
        0130-libstdc++-in-out.patch
        0240-prettify-linking-no-undefined.mingw.patch
        0408-gcc-6-branch-Windows-New-feature-to-allow-overriding.patch
        0410-fix-using-large-pch.patch
        0415-disable-weak-refs-in-libstdc++.patch
        0950-7.3.0-configure-msys2.patch
        0951-7.3.0-msys2-spec.patch
        0953-7.3.0-testsuite-msys2.patch
        0955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
        pr86138.patch)
sha256sums=('fa06e455ca198ddc11ea4ddf2a394cf7cfb66aa7e0ab98cc1184189f1d405870'
            'e2583724bbc9ac16a5a3927b79a05c006d43fd70fe0e598b5d402fb66080e308'
            '25ba33a68cefd1921d35b796a2dc033bae22b33527b8b81b04dddb3e0dee5920'
            '24bdd964c6479251af5a275965db42ed867599cd2d84ecb526397395e201cda4'
            'b77b6045aabba3aaae58d578d0b2ffc56ce70907f39a280fa964c9fcbd88bdad'
            '7ad412357429a2e2376ec673d0e0a0d2ab4e3a162db532818bc76d55835f35d6'
            'cf33d6ff5e32ed35146117c9bd3d78bb9bf228abad73e5c2b44f006bf8cf0fc8'
            '79db6a8951c3c07d9df26863a2bcfc607606211f6eb813c156b4f46123ecbbdc'
            'ac7e9e08ea8b2b4b5fdb394e217ce9081352fe07746e19b9374a753ebf89a4a2'
            '84c8127f2e70f26a6bf0f7ff1fe06df4814096bc02f5fc19291a729c740887d7'
            '4d3030238181a6d28c1aff5e5b43db6d82517f3412309175e162f5da63f7cd00'
            '0e6a373a0911d81656d9bfd2d93085b817266538600eab14965f1bafb034bac8'
            'a277245cf7ccf6e3bd83a79945ff26ea5e41fb3ecf7fe0a473b2ec64d8c5b101'
            '54a0e45c09ac70b7932cde3e86b6156c1fec459c69ece53141921adcb8a75e6e'
            '277907dd3982c61dcd8d739231e49afebf3841cf687fc1fcf3e2ffbba419c904'
            '00e528e198183799c00167cacc1a7b1cb9505ae93ef89e581632ffaae20080f2'
            '9daf3accc664d5a2b6f93354a1e8807d19c640e627dc2977111aab17b766542e'
            '5ac69b7933d576133618c4367fa54faf89e11231228f54c9ab8ce4c5fb221a49'
            '77f1ff3c35cf54c0698e80156a0b4ee0f6f87892e5b45bdafb36096c61e87fdf'
            '5c981e03ee5f34129ab06e3e4aab0bd190bf9d454d98513ac68b39e01f0b039d'
            'e1b56aa9c8786b5491ec3f6522498f4381fe78c6117b87ae0daeb7741e111a98'
            'e767610ddf54d2f277fef89e355728f31dbe9fe920eccd082942fbadfd3c1409'
            'd5dd17d7a97e8295a2ea6caa6b10f0ab384a8c1874283524242897086c07cb26'
            '018e6ef8f431c0cebbc0c01a7acc28270ba5a827904540f6b5f1c22b4955a1dd'
            'd1e10cd2ef5594c34acead334bb93af500e00699ba673edc8916da22a572677d'
            '0decbbebda7ceac91da5d3dec9b7330f406a4358fa150560f08ff6f54739035f'
            '09f27e0dae8d962f2a46a33a9891f2d14303629bb40f91ed8c5824c90da653a9'
            'd075205468c4518d7eb5d38c6c95c4bcc07b9788521cce41467b7396f44cfcb8'
            'e41e326ecfb555f527c769599537bfe312c2a7cfb899519620bfe873c228f0cb'
            '07e82b66cc4ef6f3c711525f9d91037777cad8a9f81ccf9dcecbb56193401eba'
            '89e00457776fb1cae1b020c6f835012a8617996a0d0710a4b12e9804aeda27df'
            'c2126ca50487d84fa9aaf0a64bc03df8dbc0bccc2dd5be1b5f1e277c9ad7c0aa')

_threads="win32"
_targets="i686-w64-mingw32 x86_64-w64-mingw32" #armv7-w64-mingw32 

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

apply_patch_p2_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp2 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  #do not install libiberty
  sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  #sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  # Remove files created by patches
  rm -f config/mt-cygwin \
        gcc/ada/system-cygwin-x86_64.ads \
        libgomp/config/cygwin/plugin-suffix.h \
        libgcc/config/i386/t-msys \
        gcc/config/i386/msys.h \
        gcc/config/i386/msys-w64.h \
        gcc/config/i386/t-msys-w64 > /dev/null 2>&1 | true

  # Upstream patches
  apply_patch_with_msg \
        pr86138.patch

  # Cygwin patches
  apply_patch_with_msg \
        0001-share-mingw-fset-stack-executable-with-cygwin.patch \
        0007-Avoid-installing-libffi-V2.patch \
        0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch \
        0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch \
        0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch \
        0012-handle-dllimport-properly-in-medium-model-V2.patch \
        0013-skip-test-for-cygwin-mingw.patch \
        0014-64bit-Cygwin-uses-SEH.patch \
        0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch \
        0016-fix-some-implicit-declaration-warnings.patch \
        0017-__cxa-atexit-for-Cygwin.patch \
        0018-prevent-modules-from-being-unloaded-before-their-dto.patch \
        0020-cygwin-uses-cyg-lib-prefix-v3.patch \
        0022-libgomp-soname-cygwin-mingw.patch \
        0024-libitm-weak-symbols.patch \
        0025-enable-libcilkrts-V2.patch
  apply_patch_p2_with_msg \
     0028-g++-time.patch \
     0030-newlib-ftm.patch \
     0031-define_std-unix.patch

  # MINGW patches
  apply_patch_with_msg 0130-libstdc++-in-out.patch \
    0240-prettify-linking-no-undefined.mingw.patch \
    0408-gcc-6-branch-Windows-New-feature-to-allow-overriding.patch \
    0410-fix-using-large-pch.patch \
    0415-disable-weak-refs-in-libstdc++.patch

  # MSYS2 Patches
  apply_patch_with_msg \
    0950-7.3.0-configure-msys2.patch \
    0951-7.3.0-msys2-spec.patch \
    0953-7.3.0-testsuite-msys2.patch \
    0955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
}

build() {
  for _target in ${_targets}; do
  mkdir -p ${srcdir}/gcc-build-${_target} && cd ${srcdir}/gcc-build-${_target}

  local _langs="c,lto,c++"
  case "${_target}" in
    i686*)
      local _conf="--disable-sjlj-exceptions --with-dwarf2"
      local _arch=i686
      _langs="${_langs} fortran"
    ;;

    x86_64*)
      local _arch=x86-64
      local _conf=""
      _langs="${_langs} fortran"
    ;;
  esac

	# use built-in SSP with Cygwin 2.10
	# FIXME: --disable-libssp should suffice in GCC 8
	export gcc_cv_libc_provides_ssp=yes
	# configure tries to test SUSv4-compliant behaviour of
	# realpath(..., NULL) via _XOPEN_VERSION
	export glibcxx_cv_realpath=yes
  ../${_realname}-${pkgver}/configure \
    --prefix=/opt \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${_target} \
    --libexecdir=/opt/lib \
    --disable-bootstrap \
    --with-arch=${_arch} \
    --with-tune=generic \
    --enable-languages=${_langs} \
    --enable-shared --enable-static \
    --enable-threads=${_threads} \
    --enable-graphite \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-time=yes \
    --disable-libstdcxx-pch \
    --disable-libstdcxx-debug \
    --enable-cloog-backend=isl \
    --enable-version-specific-runtime-libs \
    --disable-isl-version-check \
    --enable-lto \
    --enable-libgomp \
    --disable-multilib \
    --enable-checking=release \
    --disable-rpath \
    --disable-win32-registry \
    --disable-werror \
    --disable-symvers \
    --with-libiconv \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl}=/usr \
    --with-gnu-as --with-gnu-ld \
    ${_conf} \
    CFLAGS="-O2 -pipe" \
    CXXFALGS="-O2 -pipe"

  make all
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/gcc-build-${_target}
    make -j1 DESTDIR=${pkgdir} install
    mv ${pkgdir}/opt/lib/gcc/${_target}/lib/libgcc_s.a ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    mv ${pkgdir}/opt/lib/gcc/${_target}/libgcc*.dll ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    for _f in ${pkgdir}/opt/bin/*.exe; do
      strip -s ${_f}
    done
    for _f in ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/*.exe; do
      strip -s ${_f}
    done
    rm -rf ${pkgdir}/opt/share
  done
}
